package com.base.library.ext

import android.content.Context
import android.view.Gravity
import android.view.View
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.core.view.updatePadding
import com.base.library.BaseApplication
import com.base.library.R
import com.base.library.utils.ThemeUtils


fun Context.showToast(string: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
    val toast = Toast.makeText(applicationContext, string, duration)
    val layout = toast.view as? LinearLayout?
    //设置背景
    layout?.setBackgroundResource(R.drawable.bg_toast)
    val tv = layout?.getChildAt(0) as? TextView?
    tv?.updatePadding(
        top = R.dimen.dp_8.toDimension().toInt(),
        bottom = R.dimen.dp_8.toDimension().toInt(),
        left = R.dimen.dp_8.toDimension().toInt(),
        right = R.dimen.dp_8.toDimension().toInt()
    )
    tv?.minWidth = R.dimen.dp_120.toDimension().toInt()
    tv?.textAlignment = View.TEXT_ALIGNMENT_CENTER
    //设置字体大小
    tv?.textSize = 12f
    //设置字体颜色
    tv?.setTextColor(R.color.common_text_color.getColor())
    //显示的位置
    toast.setGravity(Gravity.CENTER, 0, 0)
    toast.show()

}

fun Int.getString() = BaseApplication.context.getString(this)
fun Int.getString(content: String) = BaseApplication.context.getString(this, content)
fun Int.getColor() = BaseApplication.context.resources.getColor(this)
fun Int.getThemeColor() = ThemeUtils.resolveColor(BaseApplication.context, this)
fun Int.getArray() = BaseApplication.context.resources.getStringArray(this)
fun Int.getDrawable() = ContextCompat.getDrawable(BaseApplication.context, this)